package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FinancialAnnouncements;
import com.ruoyi.system.service.IFinancialAnnouncementsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * announcementsController
 * 
 * @author ruoyi
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/system/announcements")
public class FinancialAnnouncementsController extends BaseController
{
    @Autowired
    private IFinancialAnnouncementsService financialAnnouncementsService;

    /**
     * 查询announcements列表
     */
    @PreAuthorize("@ss.hasPermi('system:announcements:list')")
    @GetMapping("/list")
    public TableDataInfo list(FinancialAnnouncements financialAnnouncements)
    {
        startPage();
        List<FinancialAnnouncements> list = financialAnnouncementsService.selectFinancialAnnouncementsList(financialAnnouncements);

        List<FinancialAnnouncements> newlist = new ArrayList<>();

// 按name分组
        Map<String, List<FinancialAnnouncements>> groupedByName = list.stream()
                .collect(Collectors.groupingBy(person -> person.getSECNAME()));

        // 输出分组结果
        groupedByName.forEach((name, group) -> {
            group.forEach(person -> newlist.add(person));
        });



        return getDataTable(newlist);
    }

    /**
     * 导出announcements列表
     */
    @PreAuthorize("@ss.hasPermi('system:announcements:export')")
    @Log(title = "announcements", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinancialAnnouncements financialAnnouncements)
    {
        List<FinancialAnnouncements> list = financialAnnouncementsService.selectFinancialAnnouncementsList(financialAnnouncements);
        ExcelUtil<FinancialAnnouncements> util = new ExcelUtil<FinancialAnnouncements>(FinancialAnnouncements.class);
        util.exportExcel(response, list, "announcements数据");
    }

    /**
     * 获取announcements详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:announcements:query')")
    @GetMapping(value = "/{TEXTID}")
    public AjaxResult getInfo(@PathVariable("TEXTID") String TEXTID)
    {
        return success(financialAnnouncementsService.selectFinancialAnnouncementsByTEXTID(TEXTID));
    }

    /**
     * 新增announcements
     */
    @PreAuthorize("@ss.hasPermi('system:announcements:add')")
    @Log(title = "announcements", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinancialAnnouncements financialAnnouncements)
    {
        return toAjax(financialAnnouncementsService.insertFinancialAnnouncements(financialAnnouncements));
    }

    /**
     * 修改announcements
     */
    @PreAuthorize("@ss.hasPermi('system:announcements:edit')")
    @Log(title = "announcements", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinancialAnnouncements financialAnnouncements)
    {
        return toAjax(financialAnnouncementsService.updateFinancialAnnouncements(financialAnnouncements));
    }

    /**
     * 删除announcements
     */
    @PreAuthorize("@ss.hasPermi('system:announcements:remove')")
    @Log(title = "announcements", businessType = BusinessType.DELETE)
	@DeleteMapping("/{TEXTIDs}")
    public AjaxResult remove(@PathVariable String[] TEXTIDs)
    {
        return toAjax(financialAnnouncementsService.deleteFinancialAnnouncementsByTEXTIDs(TEXTIDs));
    }
}
